#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "aht10.h"
#include "ap3216c.h"
#include "demo.h"

#define DBG_TAG "ap3216c"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>


static void ap3216c_entry(void *parameter)
{
    rt_thread_mdelay(18000);
    float upload[4];//humi,temp,brightness,distance
    aht10_device_t aht10;
    ap3216c_device_t ap3216c;

    const char *ap3216c_i2c_name = "i2c2";
    const char *aht10_i2c_name = "i2c3";

    aht10 = aht10_init(aht10_i2c_name);
    ap3216c = ap3216c_init(ap3216c_i2c_name);

    if(aht10 == RT_NULL || ap3216c == RT_NULL)
    {
        LOG_E("The sensor initializes failture\n");
    }



    rt_thread_mdelay(2000);
    while (1)
    {
        upload[0] = aht10_read_humidity(aht10);
        //LOG_D("humidity : %d %%",(int)upload[0]);

        upload[1] = aht10_read_temperature(aht10);
        //LOG_D("temperature : %d",(int)upload[1]);

        upload[2] = ap3216c_read_ambient_light(ap3216c);
        //LOG_D("brightness : %d(lux)",(int)upload[2]);

        upload[3] = ap3216c_read_ps_data(ap3216c);
        //LOG_D("distance : %d(cm)",(int)upload[3]);
        if (rt_mq_send(&mq_led, &upload[3], sizeof(float))!=RT_EOK) {
            LOG_E("put data in led failed\n");
        }else{
            if (rt_mq_send(&mq_led, &upload[3], sizeof(float))==-RT_EFULL)
            {  rt_thread_mdelay(2000);}
            else{LOG_D("data ready ,ready to send led\n");}
        }

        if (rt_mq_send(&mq, upload, sizeof(upload))!=RT_EOK) {
            LOG_E("put data in qeue failed\n");
        }else{
            if (rt_mq_send(&mq, upload, 4*sizeof(float))==-RT_EFULL) {
                rt_thread_mdelay(5000);
            }
            else{LOG_D("data ready ,ready to send\n");}
        }

        rt_thread_mdelay(500);
    }
}
